/*
 * ================================================================================变量定义区
 */
var showCount = 10; //定义每页显示条数
var currentPage = 1;//全局变量，当前页
var page_index = 0;//页面索引

/*
 * ================================================================================页面初始化函数
 */
$(function()
{
	//查看已投票，编辑问题后，点击确定以后要回到当前问题所在的页数
	var curPage = $("#answerSurvey").find("input[name=currentPage]").val();
	
	if(curPage!="")
	{
		currentPage = curPage;
	}
	
	var type = $("#type").val();
	//选择哪一张表进行展示
    if(type == "wait")
    {
    	$("#voteTableInfoName").text("待投票信息统计表");
    } 
    else if(type == "alreadyVoted")
    {
    	$("#voteTableInfoName").text("已投票信息统计表");
    } 
    else if(type == "finished")
    {
    	$("#voteTableInfoName").text("已结束投票信息统计表");
    } 
    else 
    {
    	$("#voteTableInfoName").text("没有相关信息表");
    	$("#vote").text("");
    }
	
	//初始化列表，取第一页数据
	getDataList(currentPage);
});
/*
 * ================================================================================函数定义区
 * 
 */
/**
 * 获取列表数据
 * @param currentPage 请求的页码
 */
function getDataList(pageIndex)
{
	var currentTime = new Date();
    var type = $("#type").val();
    var username = $("#username").val();
	$.ajax(
	{
		type: 'POST',
        url: BASE_PATH +'/survey/surveyList/getSurveyList.do',
        dataType:"json",
        data:
        {
        	"showCount":showCount,
        	"currentPage":pageIndex,
        	"type":type,
        	"username":username,
        	"query_condition_field_list":"type,username"
        },
        cache:false,
        success:function(data,textStatus)
        {
        	var html = "";        	
        	var pageData = data;
        	var totalResult = pageData.queryParam.totalResult;
        	if(pageData.queryResult != null)
        	{
        		//解析data, 组装成html
	        	$.each(pageData.queryResult,function(i,n)
	        	{
	        		html += '<tr onclick="gotoDetail(' + n.survey_Id + ');"  id="tr_index_' + i + '">'
        			     + '<input  name="survetyId" type="hidden" value="' + n.survey_Id + '" />'
        			     + '<td title="' + switcHtml(n.survey_Name) + '"  style="width:550px;">' + switcHtml(n.survey_Name) + '</td>'
        			     + '<td style="text-align:center;" title=';
        		
	        		if(n.status == "1" && type == "wait") 
	        		{
	        			html += "待投票";
	        	    }
	        		
	        		if(n.status == "1" && type == "alreadyVoted") 
	        		{
	        			html += "已投票";
	        	    }
	        		
	        		if(n.status == "2" && type == "finished" && changeTime(n.end_Date) >= currentTime)
	        		{
	        	    	html += "已结束";
	        	    }
	        		else if(n.status == "2" && type == "finished" && changeTime(n.end_Date) < currentTime)
	        		{
	        	    	html += "已过期";
	        	    } 
	        		
        			html += '>';
        			
        			if(n.status == "1" && type == "wait") 
        			{
	        			html += "待投票";
	        	    }
        			
        			if(n.status == "1" && type == "alreadyVoted") 
        			{
	        			html += "已投票";
	        	    }
        			
        			if(n.status == "2" && type == "finished" && changeTime(n.end_Date) >= currentTime)
        			{
	        	    	html += "已结束";
	        	    }
        			else if(n.status == "2" && type == "finished" && changeTime(n.end_Date) < currentTime)
        			{
	        	    	html += "已过期";
	        	    }
        			
        			if(n.status == "1" && type == "finished")
        			{
	        	    	html += "已过期";
	        	    }
        			
	        		html += '</td>'
		        		// + '<td style="width: 350px" title=' + switcHtml(n.remark) + '>' + switcHtml(n.remark) + '</td>'
		        		 + '<td style="text-align:center;" title=' + n.end_Date + '>' + n.end_Date + '</td>'	
		        		 + '<td style="text-align:center;" title=';
        			
        			if(n.last_Modify_Time == null)
        			{
        				html += "";
        			}
        			else 
        			{
        				html += n.last_Modify_Time;
        			}
        			html += '>';
        			
        			if( n.last_Modify_Time == null)
        			{
	        	    	html += "";
	        	    }
        			else 
	        	    {
        				html += n.last_Modify_Time;
        			}
	        		html += '</td><td style="width: 1px;"></td></tr>';
	        	}); 
        	} 
        	else
        	{
        		html = '<tr><td colspan="2">No items.</td></tr>';
        	}
        	
        	$('#tBodyList').html(html);
        	currentPage = pageIndex;
    		if(totalResult == 0)
    		{
    			$('#tBodyList').html('<tr><td style="text-align:center;">No items.</td></tr>');
    			$("#pagination").hide();
    		} 
    		else 
    		{
    			// 创建分页
    			$("#pagination").show();
    			$("#pagination").pagination(totalResult, 
    			{
    				num_edge_entries: 1, //边缘页数
    				num_display_entries: 3, //主体页数
    				current_page:currentPage,
    				callback: getDataList,
    				items_per_page:showCount //每页显示1项
    			});
    		};
    		
    		if(type != "finished")
    		{
        		$("#tBodyList tr").each(function()
        		{
            		var surveyId =$(this).find("input").val();
            		
            		if($(this).find("td").text()=="No items.")
            		{	
            		}
            		else
            		{
            			$(this).click(function()
            			{
                      		if($(this).find("td").eq(1).text().trim() == "待投票")
                      		{            				
                      			viewSurvey(surveyId);
                    		}
                      		else
                      		{
                    			answerSurvey(surveyId);
                    		}
                		});
            		}            			
            	});
    		}
    		else
    		{
    			$("#tBodyList tr").each(function()
    			{
        			var surveyId =$(this).find("input").val();
            		if($(this).find("td").text()=="No items.")
            		{	
            		}
            		else
            		{
            			$(this).click(function()
            			{
            				viewResult(surveyId);
                		});
            		} 
    			});
    	    }  	
        },
		error:function(e)
		{
			 openAlert("加载数据错误！"); 
			 return;
		}
	});
} 

/**
 * 格式化日期
 * @param date
 * @returns {String}
 */
function formatDate(date)
{
	var thisDate = new Date();
	thisDate.setTime(date);
	
	var month = parseInt(thisDate.getMonth()+1) + "";
	var day = thisDate.getDate()+ "";

	var monthStr;
	var dayStr;
	
	if ( month.length == 1)
	{
		monthStr = '0' + month;
	} 
	else 
	{
		monthStr = month;
	}

	if(day.length == 1)
	{
		dayStr = '0' + day;
	} 
	else 
	{
		dayStr = day;
	}
	
	return thisDate.getFullYear()+"-"+monthStr+"-"+dayStr;
}

/**
 * 转换成日期格式
 * @param date
 * @returns {Date}
 */
function changeTime(date) 
{
	return new Date(date);
}
/**
 * 提交form表单至已结束问卷统计结果
 * @param v 列表行对象
 */
function viewResult(surveyId)
{
	$("#viewResult [name=survetyId]").val(surveyId);
	$("#viewResult [name=type]").val("finished");
	$("#viewResult").submit();
}

/**
 * 待投票
 * @param surveyId
 */
function viewSurvey(surveyId)
{
	$("#viewSurvey [name=surveyId]").val(surveyId);
	$("#viewSurvey").submit();	
}

/**
 * 已投票
 * @param surveyId
 */
function answerSurvey(surveyId)
{
	$("#answerSurvey [name=surveyId]").val(surveyId);
	$("#answerSurvey [name=currentPage]").val(currentPage);
	$("#answerSurvey [name=type]").val("alreadyVoted");
	$("#answerSurvey").submit();	
}

/**
 * 展示问卷的详细信息
 * @param id
 */
function gotoDetail(id)
{
	var type = $("#type").val();
	if(type == "wait")
	{
		viewSurvey(id);
	}
	else if(type == "alreadyVoted")
	{
		answerSurvey(id);
	}
	else if(type == "finished")
	{
		viewResult(id);
	}
	else
	{
		openAlert('Error Param');
	}
}